package com.dsj.gxrd.modules.book.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dsj.gxrd.modules.book.domain.entity.RdBookCategory;
import com.dsj.gxrd.modules.book.domain.vo.RdBookCatTree;
import com.dsj.gxrd.modules.book.mapper.RdBookCategoryMapper;
import com.dsj.gxrd.modules.book.service.IRdBookCategoryService;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-09-11
 */
@Service
public class RdBookCategoryServiceImpl extends ServiceImpl<RdBookCategoryMapper, RdBookCategory> implements IRdBookCategoryService
{
    @Autowired
    private RdBookCategoryMapper rdBookCategoryMapper;

    /**
     * 查询【请填写功能名称】
     * 
     * @param rdBookCatId 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public RdBookCategory selectRdBookCategoryByRdBookCatId(Long rdBookCatId)
    {
        return rdBookCategoryMapper.selectById(rdBookCatId);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param rdBookCategory 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<RdBookCategory> selectRdBookCategoryList(RdBookCategory rdBookCategory)
    {
        String bookCatName = rdBookCategory.getBookCatName();
        Integer sort = rdBookCategory.getSort();
        return this.lambdaQuery().like(StringUtils.isNotEmpty(bookCatName), RdBookCategory::getBookCatName, bookCatName)
                .eq(sort != null, RdBookCategory::getSort, sort)
                .eq(RdBookCategory::getDelFlag,0)
                .orderByAsc(RdBookCategory::getSort)
                .list();
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param rdBookCategory 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertRdBookCategory(RdBookCategory rdBookCategory)
    {
        return rdBookCategoryMapper.insert(rdBookCategory);
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param rdBookCategory 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateRdBookCategory(RdBookCategory rdBookCategory)
    {
        return rdBookCategoryMapper.updateById(rdBookCategory);
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param rdBookCatIds 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteRdBookCategoryByRdBookCatIds(Long[] rdBookCatIds)
    {
        return rdBookCategoryMapper.deleteBatchIds(Arrays.asList(rdBookCatIds));
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param rdBookCatId 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteRdBookCategoryByRdBookCatId(Long rdBookCatId)
    {
        return rdBookCategoryMapper.deleteById(rdBookCatId);
    }

    @Override
    public List<RdBookCatTree> getCategoryTree(Long rdBookCatId) {
        List<RdBookCategory> list = this.lambdaQuery()
                .eq(RdBookCategory::getDelFlag, 0)
                .eq(RdBookCategory::getParentId,rdBookCatId)
                .list();
        List<RdBookCategory> allCategories = this.lambdaQuery()
                .eq(RdBookCategory::getDelFlag, 0)
                .list();
        for (RdBookCategory rdBookCategory : list) {
            List<RdBookCategory> child = findChildWithTree(rdBookCategory, allCategories);
            rdBookCategory.setChildren(child);
        }

        return list.stream().map(RdBookCatTree::new).collect(Collectors.toList());
    }


    public List<RdBookCategory> findChildWithTree(RdBookCategory rdBookCategory, List<RdBookCategory> allCategories) {
        List<RdBookCategory> children = new ArrayList<>();
        for (RdBookCategory childCategory : allCategories) {
            Long parentCid = childCategory.getParentId();
            if (rdBookCategory.getRdBookCatId().equals(parentCid)) {
                //RdBookCategory35
                children.add(childCategory);
                List<RdBookCategory> childWithTree = findChildWithTree(childCategory, allCategories);
                childCategory.setChildren(childWithTree);
            }
        }
        if(CollectionUtils.isNotEmpty(children)){
            children.sort(Comparator.comparing(RdBookCategory::getSort,(o1,o2)->{
                if(o1!=null&&o2!=null){
                    return o1-o2;
                }else {
                    return 0;
                }
            }));
        }
        return children;
    }
    
}
